package com.ruoyi.iot.dataprocessing.strategy;

import com.ruoyi.iot.dataprocessing.domain.SensorMessage;
import com.ruoyi.iot.dataprocessing.enums.DataAssemblyEnum;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Component
public class SensorMessageStrategy {
    private final Map<DataAssemblyEnum, SensorMessageHandlerService> serviceMap=new ConcurrentHashMap<>();

    public SensorMessageStrategy(List<SensorMessageHandlerService> list){
        list.forEach(service -> serviceMap.put(service.getDataAssemblyEnum(),service));
    }

    public SensorMessage getSensorMessage(Integer deviceType, byte[] payload) {
        SensorMessageHandlerService service = serviceMap.get(DataAssemblyEnum.getEnum(deviceType));
        if (service == null) {
            return null;
        }
        return service.messageHandle(payload);
    }

    public void push(Integer type,Long customerId,byte[] payload) {
        SensorMessageHandlerService service = serviceMap.get(DataAssemblyEnum.getEnum(type));
        if (service == null) {
            return;
        }
        service.push(type,customerId,payload);
    }
}
